package com.ruoyi.js.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.js.domain.JsRegisterPlayer;
import com.ruoyi.js.domain.JsRegisterReplacePlayer;
import com.ruoyi.js.domain.JsScoreQueue;
import com.ruoyi.js.mapper.JsRegisterPlayerMapper;
import com.ruoyi.js.mapper.JsRegisterReplacePlayerMapper;
import com.ruoyi.js.mapper.JsScoreQueueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.js.mapper.JsRegisterMapper;
import com.ruoyi.js.domain.JsRegister;
import com.ruoyi.js.service.IJsRegisterService;
import com.ruoyi.common.core.text.Convert;

/**
 * 报名 Service业务层处理
 * 
 * @author greejoy
 * @date 2024-04-22
 */
@Service
public class JsRegisterServiceImpl implements IJsRegisterService 
{
    @Autowired
    private JsRegisterMapper jsRegisterMapper;

    @Autowired
    private JsRegisterPlayerMapper jsRegisterPlayerMapper;

    @Autowired
    private JsRegisterReplacePlayerMapper jsRegisterReplacePlayerMapper;

    @Autowired
    private JsScoreQueueMapper jsScoreQueueMapper;

    /**
     * 查询报名 
     * 
     * @param id 报名 ID
     * @return 报名 
     */
    @Override
    public JsRegister selectJsRegisterById(Long id)
    {
        return jsRegisterMapper.selectJsRegisterById(id);
    }

    /**
     * 查询报名 列表
     * 
     * @param jsRegister 报名 
     * @return 报名 
     */
    @Override
    public List<JsRegister> selectJsRegisterList(JsRegister jsRegister)
    {
        return jsRegisterMapper.selectJsRegisterList(jsRegister);
    }

    /**
     * 新增报名 
     * 
     * @param jsRegister 报名 
     * @return 结果
     */
    @Override
    public int insertJsRegister(JsRegister jsRegister)
    {
        JsRegister existRegister = selectJsRegisterByItemPlayerIds(jsRegister.getItemId(), jsRegister.getPlayerIds());
        if(existRegister != null){
            throw new BusinessException("重复报名");
        }
        
        jsRegister.setCreateBy(ShiroUtils.getLoginName());
        jsRegister.setCreateTime(DateUtils.getNowDate());

        int res = jsRegisterMapper.insertJsRegister(jsRegister);

        if(StringUtils.isNotEmpty(jsRegister.getPlayerIds())){
            for (String s : jsRegister.getPlayerIds().split(",")) {
                jsRegisterPlayerMapper.insertJsRegisterPlayer(new JsRegisterPlayer(jsRegister.getId(), Long.valueOf(s)));
            }
        }

        if(StringUtils.isNotEmpty(jsRegister.getReplacePlayerIds())){
            for (String s : jsRegister.getReplacePlayerIds().split(",")) {
                jsRegisterReplacePlayerMapper.insertJsRegisterReplacePlayer(new JsRegisterReplacePlayer(jsRegister.getId(), Long.valueOf(s)));
            }
        }

        return res;
    }

    /**
     * 修改报名 
     * 
     * @param jsRegister 报名 
     * @return 结果
     */
    @Override
    public int updateJsRegister(JsRegister jsRegister)
    {

        JsRegister dbRegister = jsRegisterMapper.selectJsRegisterById(jsRegister.getId());
        if((jsRegister.getItemId() != null && !jsRegister.getItemId().equals(dbRegister.getItemId())) || (jsRegister.getPlayerIds() != null && !StringUtils.checkStringIds(jsRegister.getPlayerIds(), dbRegister.getPlayerIds()))){
            JsRegister existRegister = selectJsRegisterByItemPlayerIds(jsRegister.getItemId(), jsRegister.getPlayerIds());
            if(existRegister != null){
                throw new BusinessException("重复报名");
            }
        }
        
        jsRegister.setUpdateBy(ShiroUtils.getLoginName());
        jsRegister.setUpdateTime(DateUtils.getNowDate());

        int res = jsRegisterMapper.updateJsRegister(jsRegister);

        if(!StringUtils.checkStringIds(dbRegister.getPlayerIds(), jsRegister.getPlayerIds())){
            jsRegisterPlayerMapper.deleteJsRegisterPlayerByRegisterId(jsRegister.getId());
            if(StringUtils.isNotEmpty(jsRegister.getPlayerIds())){
                for (String s : jsRegister.getPlayerIds().split(",")) {
                    jsRegisterPlayerMapper.insertJsRegisterPlayer(new JsRegisterPlayer(jsRegister.getId(), Long.valueOf(s)));
                }
            }
        }

        if(!StringUtils.checkStringIds(jsRegister.getReplacePlayerIds(), dbRegister.getReplacePlayerIds())){
            jsRegisterReplacePlayerMapper.deleteJsRegisterReplacePlayerByRegisterId(jsRegister.getId());
            if(StringUtils.isNotEmpty(jsRegister.getReplacePlayerIds())){
                for (String s : jsRegister.getReplacePlayerIds().split(",")) {
                    jsRegisterReplacePlayerMapper.insertJsRegisterReplacePlayer(new JsRegisterReplacePlayer(jsRegister.getId(), Long.valueOf(s)));
                }
            }
        }

        return res;
    }

    /**
     * 删除报名 对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJsRegisterByIds(String ids)
    {
        return jsRegisterMapper.deleteJsRegisterByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除报名 信息
     * 
     * @param id 报名 ID
     * @return 结果
     */
    @Override
    public int deleteJsRegisterById(Long id)
    {
        return jsRegisterMapper.deleteJsRegisterById(id);
    }

    @Override
    public JsRegister selectJsRegisterByItemPlayerIds(Long itemId, String playerIds) {
        List<JsRegister> registerList = jsRegisterMapper.selectJsRegisterList(new JsRegister(itemId));
        JsRegister register = registerList.stream().filter(f -> StringUtils.checkStringIds(f.getPlayerIds(), playerIds)).findFirst().orElse(null);
        return register;
    }

    @Override
    public List<JsRegister> selectJsRegisterListToMatch(JsRegister jsRegister)
    {
        return jsRegisterMapper.selectJsRegisterListToMatch(jsRegister);
    }

    @Override
    public synchronized boolean checkAllJudgeScored(Long registerId) {
        boolean result = false;
        List<JsScoreQueue> scoreQueueList = jsScoreQueueMapper.selectJsScoreQueueList(new JsScoreQueue(registerId, null));
        List<JsScoreQueue> scoredScoreQueueList  = scoreQueueList.stream().filter(f -> f.getIsScored()).collect(Collectors.toList());
        if(scoreQueueList.size() == scoredScoreQueueList.size()){
            JsRegister register = jsRegisterMapper.selectJsRegisterById(registerId);
            if(!register.getIsScored()){
                register.setIsScored(true);
                jsRegisterMapper.updateJsRegister(register);
            }
            result = true;
        }
        return  result;
    }

}
